package com.gd.coreservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gd.coreservice.entity.Admin;
import com.gd.coreservice.entity.Goods;
import com.gd.coreservice.mapper.GoodsMapper;
import com.gd.coreservice.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-03-23
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Override
    public List<Goods> findAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public List<Goods> getUidGoods(String uid, String status, String name) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seller_id", uid)
                .like("status", status)
                .like("item_name", name)
                .orderByDesc("created_at");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Integer getSidTotal(String uid, String status, String name) {
        return baseMapper.gettotal(uid,status,name);
    }

    @Override
    public Goods findOne(String gid) {
        return baseMapper.selectById(gid);
    }

    @Override
    public boolean insertGoods(Goods goods) {
        int result = 0;
        try {
            result = baseMapper.insert(goods);
        }catch (Exception e){

        }
        if (0!=result)
            return true;
        else return false;
    }

    @Override
    public boolean updateGoodsGstatus(String gid, String status) {
        Goods goods = new Goods();
        goods.setGid(gid);
        goods.setStatus(status);
        int i = 0;
        try {
             i = baseMapper.updateById(goods);
        }catch (Exception e){

        }
        if (0!=i)
            return true;
        else return false;
    }

    @Override
    public boolean updateGoods(Goods goods) {
        int i = 0;
        try {
            i = baseMapper.updateById(goods);
        }catch (Exception e){

        }
        if (0!=i)
            return true;
        else return false;
    }

    @Override
    public boolean updateStudentGoods(Goods goods) {
        return updateGoods(goods);
    }

    @Override
    public Integer deleteGoods(String gid) {
        return baseMapper.deleteById(gid);
    }

    @Override
    public Integer deleteGoodsBatch(List<String> gids) {
        return baseMapper.deleteBatchIds(gids);
    }

    @Override
    public Map<String, Object> getPageMap(Integer pageNum, Integer pageSize, String gname) {
        Page<Goods> goodsPage = new Page<>(pageNum,pageSize);
        Map<String,Object> reslutMap = new HashMap<>();

        QueryWrapper<Goods> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("item_name",gname)
        .eq("is_approved",'1');  //比起下面的管理员的分页查询多了个这个条件  注意这里的1必须是'1'不然不起作用
        IPage<Goods> goodsIPage = baseMapper.selectPage(goodsPage, objectQueryWrapper);

        reslutMap.put("data",goodsIPage.getRecords());
        reslutMap.put("total",goodsIPage.getTotal());
        return reslutMap;
    }

    @Override
    public Map<String, Object> getAdminPageMap(Integer pageNum, Integer pageSize, String gname) {
        Page<Goods> goodsPage = new Page<>(pageNum,pageSize);
        Map<String,Object> reslutMap = new HashMap<>();

        QueryWrapper<Goods> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("item_name",gname);
        IPage<Goods> goodsIPage = baseMapper.selectPage(goodsPage, objectQueryWrapper);

        reslutMap.put("data",goodsIPage.getRecords());
        reslutMap.put("total",goodsIPage.getTotal());
        return reslutMap;
    }

    @Override
    public int getAllTotal() {
        return baseMapper.selectCount(null);
    }
}
